package com.zzw.aliyun.green.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.aliyun.green20220302.models.TextModerationResponse;
import com.aliyun.tea.TeaException;
import com.zzw.aliyun.green.common.exception.AliyunGreenException;
import com.zzw.aliyun.green.config.AliyunGreenConfigProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@Slf4j
@Component
public class GreenTextScanNew {

    @Autowired
    private com.aliyun.green20220302.Client client;

    public Map greeTextScan(String content) throws Exception {
        com.aliyun.green20220302.models.TextModerationRequest textModerationRequest = new com.aliyun.green20220302.models.TextModerationRequest();

        /**
         * 待检测的文本，长度不超过10000个字符
         */
        JSONObject tasks = new JSONObject();
        tasks.set("content", content);
        textModerationRequest.setServiceParameters(tasks.toString());
        /**
         * 检测场景，文本垃圾检测传递：antispam
         **/
        textModerationRequest.setService("chat_detection");

        Map<String, String> resultMap = new HashMap<>();

        try {
            // 复制代码运行请自行打印 API 的返回值
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            TextModerationResponse textModerationResponse = client.textModerationWithOptions(textModerationRequest, runtime);
            if (textModerationResponse.statusCode == 200) {
                JSONObject body = new JSONObject(textModerationResponse.getBody());
//                System.out.println(body.toString());
                String code = body.get("code", String.class);
                if ("200".equals(code)) {
                    resultMap.put("status", "1");
                    JSONObject data = body.get("data", JSONObject.class);
                    String labels = data.get("labels", String.class);
                    String reason = data.get("reason", String.class);

                    if (StrUtil.isBlank(labels) && StrUtil.isBlank(reason)) {
                        resultMap.put("suggestion", "pass");
                        return resultMap;
                    }

                    if (StrUtil.isNotBlank(labels)){
                        resultMap.put("label", labels);
                    }
                    if (StrUtil.isNotBlank(reason)){
                        JSONObject reasonJson = new JSONObject(reason);
                        resultMap.put("riskTips", reasonJson.get("riskTips", String.class));
                        resultMap.put("riskWords", reasonJson.get("riskWords", String.class));
                    }
                    resultMap.put("suggestion", "forbid");
                    return resultMap;
                } else {
                    resultMap.put("status", "0");
                    return resultMap;
                }
            } else {
                resultMap.put("status", "0");
                return resultMap;
            }
        } catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new AliyunGreenException(error.getMessage());
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new AliyunGreenException(error.getMessage());
        }
    }



}